<?php
namespace Admin\Controller;
use Think\Controller;
class CropsTypeController extends Controller{
    public function index(){
        $this->display();
    }
    // public function CropsTypeAdd(){

    //     $id=$_GET['id'];
    //     if($id){
    //         $type=M('labor_skill');
    //         $list=$type->find($id);
    //         if(strlen($list['pid'])>0 && isset($list['path'])){
    //             // $pid=$_GET['pid'];
    //             $list['path']=$list['path'].$id.",";
    //         }
    //     }else{
    //             $list['pid']=0;
    //             $list['path']=$list['pid'].',';
    //             $list['name']='';
    //         }
        
    //     // dump($list);exit;
    //     $type=M('labor_skill');
    //     if(IS_POST){
    //         $info=$type->create();          
    //         if($info){
    //             if($type->add()){
    //                 $this->success('添加成功','index');
    //             }
    //         }else{
    //             $this->error($type->getError());
    //         }
    //     }else{

    //         $this->assign('list',$list);
    //         $this->display();
    //     }
    //     $this->display();
    // }
    
    public function CropsTypeList(){
        $type=M('crops_type');
        $count=$type->count();
        $typelist=$type->select();
        //var_dump($typelist);
        $this->assign('typelist',$typelist);
        $this->display();
        
    }
    public function CropsTypeAdd(){
       $a = I('ct_name');
        $mod = new \Think\Model("crops_type");
        /*var_dump($_POST);
        die();*/
        // ->where('st_fid=0')
     $list = M('crops_type')->select();
      
       if($a != null){
        // $_POST['st_id'] = I['st_name'];
         $asd['ct_f_id'] = I('pid');
        $asd['ct_name'] = I('ct_name');
        $asd['ct_status'] = I('ct_status');
        // var_dump($asd);
        // die();
    
            $mod->create();     //用于将表单传递过来的数据打包
            $re = $mod->add($asd);
            if($re){
                $this->success("添加成功", "");
            }else{
                $this->error("添加失败","");
            }
        }else{
            $data = array();
            foreach($list as $k => $v){
                if($v['ct_f_id'] == 0){
                    $data[]=$v;
                    foreach($list as $k1 => $v1){
                        if($v['ct_id'] == $v1['ct_f_id']){
                            $v1['ct_name'] = '---'.$v1['ct_name'];
                            $data[] = $v1;
                            foreach ($list as $key => $value) {
                                if($v1['ct_id'] == $value['ct_f_id']){
                                    $value['ct_name'] = '------'.$value['ct_name'];
                                    $data[]=$value;
                                }
                            }
                        }
                    }
                }
            }

// var_dump($data);
         // $this->assign ( "info", $info);
            $this->assign ( "data", $data);

               $this->display();
            
        }

        
    }
}